package laMalinche.test.annotatedRules;

import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.junit.Assert;
import laMalinche.editableContext.BasicAboutToChangeManager;
import laMalinche.propertyChangeAdapters.model.AccessReporterImp;
import laMalinche.LaMalinche;
import laMalinche.BasicLaMa;
import laMalinche.LaMaContext;
import laMalinche.AnnotatedRules.InScopeOf;
import laMalinche.AnnotatedRules.AnnotatedMethodsClass;
import laMalinche.testingPlayGround.testModel.PersonVo;
import laMalinche.rete.nodes.tailing.AccessReporter;
import laMalinche.rete.rule.ProductionRule;

import java.util.Set;
import java.beans.PropertyChangeListener;

/**
 * Created by IntelliJ IDEA.
 * User: ranran
 * Date: Aug 15, 2009
 * Time: 11:11:34 AM
 * To change this template use File | Settings | File Templates.
 */
public class SimpleLamaNoFlow {
    protected BasicAboutToChangeManager aboutToChangeManager;
    protected LaMalinche laMalinche;
    protected AccessReporter accessReporterImp;
    protected PersonVo person;

    @Before
    public void before() {
        aboutToChangeManager = new BasicAboutToChangeManager();
        accessReporterImp = new AccessReporterImp();
        makeModel();
        makeLaMa();
    }

    private void makeModel() {
        person = new PersonVo();
        person.setAccessReporter((PropertyChangeListener) accessReporterImp);
        person.setAge(0);
        PersonVo pF = new PersonVo();
        pF.setAge(3);
        person.setFather(pF);
        PersonVo mother = new PersonVo();
        mother.setAge(13);
        person.setMother(mother);
    }

    @After
    public void after() {
        if (laMalinche != null) laMalinche.detach();
        accessReporterImp = null;
        if (person != null) person.detach();
//		BasicConstraintManager.newInstance().detach();
    }

    @Test
    public void testAddRule() {
		laMalinche.start();
		Assert.assertEquals(person.getAge(), 13);
        Assert.assertEquals(person.getFather().getAge(), 18);
        Assert.assertEquals(person.getMother().getAge(), 23);
        person.setAge(45);
        Assert.assertEquals(person.getAge(), 13);
        Assert.assertEquals(person.getFather().getAge(), 18);     
	}

    protected LaMalinche makeLaMa() {
        laMalinche = new BasicLaMa();
        laMalinche.setAboutToChangeManager(aboutToChangeManager);
        laMalinche.add(createteProductionRules());
        LaMaContext.getInstance().setAccessReporter(accessReporterImp);
        return laMalinche;
    }

    private Set<ProductionRule> createteProductionRules() {
        AnnotatedMethodsClass<PersonVo> methodsClass = new AnnotatedMethodsClass<PersonVo>(person, ClassWithRules.class);
        return methodsClass.getProductionRules();
    }
}
